import React from "react";
import { Link, Route, Switch } from "react-router-dom";

// 动态路由的写法和vue是完全一样的 也是/detail/:id

class Home extends React.Component {
  render() {
    return (
      <>
        <div>home页面</div>
      </>
    );
  }
}

const About = () => {
  return <div>about页面</div>;
};

const Detail = (props) => {
  // console.log(props.match.params.id);

  // ?.表示可选链操作符
  const id = props.match?.params?.id;

  return <div>详情页页面 - {id}</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v5-动态路由</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail/234">详情页</Link>
        </li>
        <li>
          <Link to="/detail/567">详情页</Link>
        </li>
      </ul>

      <hr />

      <Switch>
        <Route path="/home" component={Home}></Route>
        <Route path="/about" component={About}></Route>
        <Route path="/detail/:id" component={Detail}></Route>
      </Switch>
    </>
  );
};

export default App;
